﻿using System;
using System.IO;
using System.Security;

namespace Exception2
{
    public sealed class PainLess
    {
        /// <summary>
        /// 1. The numeric integer error codes that utterly failed to describe the errors they represented (e.g. what does 2342 mean?) 
        /// 2. separate the essential functionality from the error handling
        /// 3. The exception classes are not tightly coupled to each other. 
        ///    In contrast, each error integer code must hold a unique value thus coupling all the error codes together. 
        ///    
        /// The problem is that 
        /// if an exception occurs after acquiring the resource but before releasing it 
        /// then the release will not take place. 
        /// </summary>
        /// <param name="args"></param>
        public static void Main(string[] args)
        {
            try
            {
               string filename = args[0];
               char[] source = ReadSource(filename);
               Process(filename, source);               
            }
            catch (SecurityException    caught) {}
            catch (IOException          caught) {}
            catch (OutOfMemoryException caught) {}            
        }

        private static void Process(string filename, char[] source)
        {
            throw new NotImplementedException();
        }

        private static char[] ReadSource(string filename)
        {
            FileInfo file = new FileInfo(filename);
            int length = (int)file.Length;
            char[] source = new char[length];
            TextReader reader = file.OpenText();
            // The problem is that if an exception occurs after acquiring the resource but before releasing it 
            // then the release will not take place. 
            reader.Read(source, 0, length);
            reader.Close();
            return source;
        }
    }
}
